
var mongoose = require('./model');

// 骨架，只有表结构，没有curd行为
var usersSchema = new mongoose.Schema({
    email: {type: String},
    password: {type: String},
    last_login_time: {type: Date, default: new Date()}
});

// 创建users模型
var usersModel = mongoose.model('user', usersSchema);

//判断用户是否存在
exports.exit=function(exitEail,callback) {
    usersModel.find(exitEail, function (err, doc) {
        if (err){
            throw err;
        }else{
            for(var k in doc){
                    if(doc[k].email==exitEail){
                        callback(1);
                        return false
                    }else{
                        callback(0)
                    }

            }
        }

    })
};

//注册用户
exports.addUser = function (addUser,callback) {
    usersModel.create(addUser, function (err) {
        if (err){
            throw err;
        }else{
            console.log('数据添加成功');
            callback(1)
        }

    })
};

//登录用户
exports.login = function (user, callback) {
    usersModel.find(user, function (err, doc) {
        if (err){
            throw err;
        }else{
            for(var k in doc){
                if(doc[k].email==user.email&&doc[k].password==user.password){
                    callback(1);
                    return false
                }else{
                    callback(0)
                }

            }
        }

    })

};
